import { request, downloadBlobFile } from 'src/utils';

export interface DictItemDto {
  dictItemId: IdType; // 字典明细ID
  dictId: number; // 字典ID
  dictCode: string; // 字典编码
  dictValue: string; // 字典键值
  dictLabel: string; // 字典标签
  sortNo: number; // 排序号
  remark: string; // 备注说明
  disabled: boolean; // 是否作废
  createdBy: string; // 创建人
  createdTime: Date; // 创建时间
  updatedBy: string; // 修改人
  updatedTime: Date; // 修改时间
}

const baseUrl = '/api/system/dictItem';

/**
 * 查单个
 */
export function readData(dictItemId: IdType): Promise<DictItemDto> {
  return request.get(baseUrl + '/' + dictItemId);
}

/**
 * 查集合
 */
export function queryList(query: PageQuery): Promise<PageResult<DictItemDto>> {
  return request.get(baseUrl, { params: query });
}

/**
 * 新增，修改
 */
export function saveOrUpdate(dictItem: DictItemDto): Promise<DictItemDto> {
  return dictItem.dictItemId ? request.put(baseUrl + '/' + dictItem.dictItemId, dictItem) : request.post(baseUrl, dictItem);
}

/**
 * 删除
 */
export function deleteByIds(ids: IdType[]): Promise<void> {
  return request.delete(baseUrl, { params: { ids } });
}

/**
 * 导出
 */
export function exportData(query: PageQuery): Promise<void> {
  return new Promise((resolve, reject) => {
    request
      .get(baseUrl + '/export', { params: query, responseType: 'blob' })
      .then((res) => {
        downloadBlobFile(res);
        resolve();
      })
      .catch((err) => reject(err));
  });
}

export default { readData, queryList, saveOrUpdate, deleteByIds, exportData } as CrudApi<DictItemDto>;
